package com.group.activity.controller;

import com.group.activity.entity.ShopOrderEntity;
import com.group.activity.service.ShopOrderService;
import com.group.activity.vo.PayAsyncVo;
import com.group.common.utils.R;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.List;


/**
 * 订单表
 *
 * @author gxx
 * @email hongyezhaofang.com
 * @date 2021-11-13 10:36:51
 */
@RestController
@RequestMapping("activity/shoporder")
public class ShopOrderController {
    @Autowired
    private ShopOrderService shopOrderService;
    /*
    * 获取当前登录用户的所有订单
    * */
    @RequestMapping("/getOrderList")
    public R getOrderList(){
        List<ShopOrderEntity> orderEntities = shopOrderService.getOrderList();
        return R.ok().setData(orderEntities);
    }

    /*
    * 根据订单号查找订单
    * */
    @RequestMapping("/getOrderInfoByOrderSn/{orderSn}")
    public R getOrderInfoByOrderSn(@PathVariable("orderSn") String orderSn){
        ShopOrderEntity orderEntity = shopOrderService.getOrderInfoByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }

    @Autowired(required = false)
    private RedissonClient redisson;

    private final String KEY = "USER.TOKEN";

    @RequestMapping("notify")
    public String notifyorder(@RequestBody PayAsyncVo payAsyncVo){
        return shopOrderService.notifyorder(payAsyncVo);
    }

    /*
     * 提交订单
     * */
    @RequestMapping("submitOrder")
    public R submitOrder(@RequestBody ShopOrderEntity shopOrderEntity){
        return shopOrderService.submitOrder(shopOrderEntity);
    }


    /*
    删除
     */
    @RequestMapping("/delete/{orderId}")
    public R delete(@PathVariable("orderId")Integer orderId){
        if (orderId==null){
            return R.error("ID不能为空");
        }
        // 删除之前先查询
        ShopOrderEntity shopOrderEntity = shopOrderService.getById(orderId);
        if (shopOrderEntity==null){
            return R.ok("用户已经删除了");
        }
        boolean b = shopOrderService.removeById(orderId);
        if (b){
            return R.ok("删除用户成功");
        }
        return R.error("删除用户失败");
    }

    /*
    修改
     */
    @PutMapping("/update")
    public R update(@RequestBody ShopOrderEntity shopOrderEntity){
        sleep();
        R r = checkUserBoUpdate(shopOrderEntity);
        if (!r.isEmpty()){
            return r;
        }
        shopOrderService.updateById(shopOrderEntity);

        return R.ok("更新用户成功");
    }


    /*
    保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ShopOrderEntity shopOrderEntity){
        shopOrderService.save(shopOrderEntity);

        return R.ok();
    }

    private void sleep(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private R checkUserBoUpdate(ShopOrderEntity shopOrderEntity){
        if (shopOrderEntity.getOrderId()==null){
            return R.error("ID不能为空");
        }
        if (shopOrderEntity.getOrderSn()==null){
            return R.error("订单号不能为空");
        }
        if (shopOrderEntity.getOrderDate()==null){
            return R.error("订单时间不能为空");
        }
        if (shopOrderEntity.getOrderPay()==null){
            return R.error("订单支付方式不能为空");
        }
        if (shopOrderEntity.getPrderHeadId()==null){
            return R.error("团长id不能为空");
        }
        if (shopOrderEntity.getOrderBuyersId()==null){
            return R.error("买家信息不能为空");
        }
        if (shopOrderEntity.getOrderSellerId()==null){
            return R.error("商家信息不能为空");
        }
        if (shopOrderEntity.getOrderDeliveryWay()==null){
            return R.error("配送方式不能为空");
        }
        if (shopOrderEntity.getOrderPayPrice()==null){
            return R.error("支付金额不能为空");
        }
        if (shopOrderEntity.getOrderDeliveryPrice()==null){
            return R.error("配送费不能为空");
        }
        if (shopOrderEntity.getOrderStatus()==null){
            return R.error("订单状态不能为空");
        }
        if (shopOrderEntity.getOrderNumber()==null){
            return R.error("商品购买数量不能为空");
        }
        if (shopOrderEntity.getSkuId()==null){
            return R.error("商品id不能为空");
        }
        return R.ok();
    }
}
